package com.sy.olddriver.model;

import com.sy.olddriver.annotation.Controller;
import com.sy.olddriver.annotation.RequestMapping;
import com.sy.olddriver.annotation.ResponseBody;
import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.dto.Page;
import com.sy.olddriver.entity.Product;
import com.sy.olddriver.entity.User;
import com.sy.olddriver.service.HistoryService;
import com.sy.olddriver.service.ProductService;
import com.sy.olddriver.service.UserService;
import com.sy.olddriver.service.impl.HistoryServiceImpl;
import com.sy.olddriver.service.impl.ProductServiceImpl;
import com.sy.olddriver.service.impl.UserServiceImpl;
import com.sy.olddriver.utils.CookieUtils;
import com.sy.olddriver.utils.RedisUtils;
import com.sy.olddriver.vo.ProductQueryParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
@RequestMapping("product")
public class ProductModel {
    private ProductService productService = new ProductServiceImpl();
    private HistoryService historyService = new HistoryServiceImpl();

    @RequestMapping("getNewestProducts")
    @ResponseBody
    public List<Product> getNewestProducts() {
        return productService.getNewestProducts();
    }

    @RequestMapping("getHotestProducts")
    @ResponseBody
    public List<Product> getHotestProducts() {
        return productService.getHotestProducts();
    }

    @RequestMapping("getPagingProducts")
    @ResponseBody
    public Page<Product> getPagingProducts(HttpServletRequest req) {
        String q = req.getParameter("q");
        String cidStr = req.getParameter("cid");
        String pageNo = req.getParameter("pageNo");
        ProductQueryParam param = new ProductQueryParam();
        param.setQ(q);
        param.setCid(StringUtils.isBlank(cidStr) ? null : Integer.parseInt(cidStr));
        return productService.getPagingProducts(param, StringUtils.isBlank(pageNo) ? null : Long.parseLong(pageNo));
    }

    @RequestMapping("getProductById")
    @ResponseBody
    public Product getProductById(HttpServletRequest req, HttpServletResponse res) {
        String pid = req.getParameter("pid");
        //添加历史记录
        historyService.addHistory(pid, req, res);
        return productService.getProductById(Integer.parseInt(pid));
    }


}
